package com.yao.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.config.BusinessException;
import com.yao.mall.mapper.CategoryMapper;
import com.yao.mall.mapper.GoodsMapper;
import com.yao.mall.model.dto.BrandRequest;
import com.yao.mall.model.dto.BrandSearchRequest;
import com.yao.mall.model.entity.Brand;
import com.yao.mall.model.entity.Category;
import com.yao.mall.model.entity.Goods;
import com.yao.mall.model.entity.User;
import com.yao.mall.model.vo.BrandVO;
import com.yao.mall.model.vo.UserVO;
import com.yao.mall.service.BrandService;
import com.yao.mall.mapper.BrandMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiahua.yao
 * @description 针对表【brand】的数据库操作Service实现
 * @date 2024-03-07 16:47:35
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
        implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public Page<BrandVO> getBrandList(BrandSearchRequest brandSearchRequest) {
        long pageSize = brandSearchRequest.getPageSize();
        long current = brandSearchRequest.getCurrent();
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        String nameEn = brandSearchRequest.getNameEn();
        if(StringUtils.isNotBlank(nameEn)){
            brandQueryWrapper.like("name_en",nameEn);
        }
        String name = brandSearchRequest.getName();
        if(StringUtils.isNotBlank(name)){
            brandQueryWrapper.like("name",name);
        }
        PageDTO<Brand> userRecords = this.page(new PageDTO<>(current, pageSize), brandQueryWrapper);
        List<Brand> records = userRecords.getRecords();
        List<BrandVO> brandVOList = toBrandVO(records);
        PageDTO<BrandVO> brandVOPageDTO = new PageDTO<>(userRecords.getCurrent(),userRecords.getSize(), userRecords.getTotal());
        brandVOPageDTO.setRecords(brandVOList);
        return brandVOPageDTO;
    }

    @Override
    public List<BrandVO> getBrandByCateId(Long cateId) {
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        brandQueryWrapper.eq("category_id",cateId);
        List<Brand> brands = brandMapper.selectList(brandQueryWrapper);
        return toBrandVO(brands);
    }

    @Override
    public List<BrandVO> toBrandVO(List<Brand> list) {
        return list.stream().map(brand -> {
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(brand, brandVO);
            Long categoryId = brand.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            brandVO.setCategory(category);
            return brandVO;
        }).toList();
    }

    @Override
    public Boolean addBrand(BrandRequest brandRequest) {
        //1.非空判断
        check(brandRequest);
        //2.add
        Brand brand = new Brand();
        if(StringUtils.isEmpty(brandRequest.getLogo())){
            throw new BusinessException(ErrorCode.NULL_ERROR, "图片不能为空");
        }
        BeanUtils.copyProperties(brandRequest, brand);
        return this.save(brand);
    }

    private static void check(BrandRequest brandRequest) {
        if (StringUtils.isAnyBlank(brandRequest.getName(),
                brandRequest.getFoundedTime(), brandRequest.getCountry())) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "存在参数为空");
        }
    }

    @Override
    public Boolean deleteBrand(Long id) {
        //注意如果有商品关联该品牌，则该品牌不可以删除
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("brand_id", id);
        if (goodsMapper.selectCount(goodsQueryWrapper) > 0) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "有该品牌商品存在，不可删除！");
        }
        int i = brandMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public Boolean updateBasic(BrandRequest brandRequest) {
        check(brandRequest);
        if(brandRequest.getId() == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"商品图片id为空");
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandRequest,brand);
        return this.updateById(brand);
    }


}




